package com.bmslz.monitor.service.impl;

import com.bmslz.monitor.base.CommonConstant;
import com.bmslz.monitor.service.RuleDataCompare;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class RuleDataCompareNumber implements RuleDataCompare {

    @Override
    public boolean supports(String dataType) {
        return CommonConstant.ATT_KEY_TYPE_NUMBER.equals(dataType);
    }

    @Override
    public boolean compare(String condition, String modelValue, String attValue) {
        BigDecimal modelValueDecimal = new BigDecimal(modelValue);
        BigDecimal attValueDecimal = new BigDecimal(attValue);
        // 使用compareTo进行精确比较
        int comparisonResult = attValueDecimal.compareTo(modelValueDecimal);
        return switch (condition) {
            case CommonConstant.ALARM_RULE_COMPARE_CONDITION_GT -> comparisonResult > 0;
            case CommonConstant.ALARM_RULE_COMPARE_CONDITION_GE -> comparisonResult >= 0;
            case CommonConstant.ALARM_RULE_COMPARE_CONDITION_EQ -> comparisonResult == 0;
            case CommonConstant.ALARM_RULE_COMPARE_CONDITION_LT -> comparisonResult < 0;
            case CommonConstant.ALARM_RULE_COMPARE_CONDITION_LE -> comparisonResult <= 0;
            default -> false;
        };
    }
}
